using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace F1RacingGame
{
    public class MainMenuScreen : IGameScreen
    {
        private int selectedOption = 0;
        private string[] menuOptions = { "开始游戏", "设置", "排行榜", "退出游戏" };
        private Font titleFont;
        private Font menuFont;
        private Font selectedFont;
        private SolidBrush titleBrush;
        private SolidBrush menuBrush;
        private SolidBrush selectedBrush;
        private SolidBrush backgroundBrush;
        
        // 自动演示相关
        private bool isDemoMode = false;
        private int demoTimer = 0;
        private const int DEMO_DELAY = 300; // 5秒后开始演示
        
        public void Initialize()
        {
            titleFont = new Font("Arial", 48, FontStyle.Bold);
            menuFont = new Font("Arial", 20, FontStyle.Regular);
            selectedFont = new Font("Arial", 22, FontStyle.Bold);
            
            titleBrush = new SolidBrush(Color.FromArgb(255, 255, 215, 0)); // 金色
            menuBrush = new SolidBrush(Color.FromArgb(255, 255, 255, 255)); // 白色
            selectedBrush = new SolidBrush(Color.FromArgb(255, 255, 69, 0)); // 橙红色
            backgroundBrush = new SolidBrush(Color.FromArgb(255, 0, 0, 50)); // 深蓝色背景
            
            selectedOption = 0;
            demoTimer = 0;
            isDemoMode = false;
        }
        
        public void Update()
        {
            // 自动演示逻辑
            if (!isDemoMode)
            {
                demoTimer++;
                if (demoTimer >= DEMO_DELAY)
                {
                    isDemoMode = true;
                    // 这里可以触发自动演示
                }
            }
        }
        
        public void Render(Graphics graphics)
        {
            // 确保资源已初始化
            if (backgroundBrush == null || titleFont == null || titleBrush == null)
            {
                Initialize();
            }
            
            // 绘制渐变背景
            DrawGradientBackground(graphics);
            
            // 绘制装饰性边框
            DrawDecorativeBorder(graphics);
            
            // 绘制标题
            string title = "F1 RACING GAME";
            SizeF titleSize = graphics.MeasureString(title, titleFont);
            float titleX = (GameEngine.GAME_WIDTH - titleSize.Width) / 2;
            
            // 绘制标题阴影
            SolidBrush shadowBrush = new SolidBrush(Color.FromArgb(100, 0, 0, 0));
            graphics.DrawString(title, titleFont, shadowBrush, titleX + 3, 103);
            shadowBrush.Dispose();
            
            // 绘制主标题
            graphics.DrawString(title, titleFont, titleBrush, titleX, 100);
            
            // 绘制副标题
            string subtitle = "CLASSIC RACING EXPERIENCE";
            Font subtitleFont = new Font("Arial", 14, FontStyle.Italic);
            SolidBrush subtitleBrush = new SolidBrush(Color.FromArgb(200, 255, 255, 255));
            SizeF subtitleSize = graphics.MeasureString(subtitle, subtitleFont);
            float subtitleX = (GameEngine.GAME_WIDTH - subtitleSize.Width) / 2;
            graphics.DrawString(subtitle, subtitleFont, subtitleBrush, subtitleX, 160);
            subtitleFont.Dispose();
            subtitleBrush.Dispose();
            
            // 绘制菜单选项
            float menuY = 280;
            for (int i = 0; i < menuOptions.Length; i++)
            {
                Font currentFont = (i == selectedOption) ? selectedFont : menuFont;
                SolidBrush currentBrush = (i == selectedOption) ? selectedBrush : menuBrush;
                
                SizeF optionSize = graphics.MeasureString(menuOptions[i], currentFont);
                float optionX = (GameEngine.GAME_WIDTH - optionSize.Width) / 2;
                
                // 绘制选中项的装饰
                if (i == selectedOption)
                {
                    DrawSelectionIndicator(graphics, optionX - 30, menuY, optionSize.Width + 60);
                }
                
                graphics.DrawString(menuOptions[i], currentFont, currentBrush, optionX, menuY);
                menuY += 60;
            }
            
            // 绘制提示信息
            string hint = "使用方向键选择，回车键确认";
            Font hintFont = new Font("Arial", 12, FontStyle.Regular);
            SolidBrush hintBrush = new SolidBrush(Color.FromArgb(180, 255, 255, 255));
            SizeF hintSize = graphics.MeasureString(hint, hintFont);
            float hintX = (GameEngine.GAME_WIDTH - hintSize.Width) / 2;
            graphics.DrawString(hint, hintFont, hintBrush, hintX, GameEngine.GAME_HEIGHT - 60);
            
            hintFont.Dispose();
            hintBrush.Dispose();
        }
        
        private void DrawGradientBackground(Graphics graphics)
        {
            // 创建渐变背景
            Rectangle rect = new Rectangle(0, 0, GameEngine.GAME_WIDTH, GameEngine.GAME_HEIGHT);
            using (LinearGradientBrush gradientBrush = new LinearGradientBrush(
                rect, 
                Color.FromArgb(255, 0, 0, 80), 
                Color.FromArgb(255, 0, 0, 30), 
                90f))
            {
                graphics.FillRectangle(gradientBrush, rect);
            }
        }
        
        private void DrawDecorativeBorder(Graphics graphics)
        {
            // 绘制装饰性边框
            Pen borderPen = new Pen(Color.FromArgb(150, 255, 215, 0), 3);
            graphics.DrawRectangle(borderPen, 20, 20, GameEngine.GAME_WIDTH - 40, GameEngine.GAME_HEIGHT - 40);
            
            // 绘制角落装饰
            int cornerSize = 20;
            graphics.DrawLine(borderPen, 20, 20, 20 + cornerSize, 20);
            graphics.DrawLine(borderPen, 20, 20, 20, 20 + cornerSize);
            
            graphics.DrawLine(borderPen, GameEngine.GAME_WIDTH - 20, 20, GameEngine.GAME_WIDTH - 20 - cornerSize, 20);
            graphics.DrawLine(borderPen, GameEngine.GAME_WIDTH - 20, 20, GameEngine.GAME_WIDTH - 20, 20 + cornerSize);
            
            graphics.DrawLine(borderPen, 20, GameEngine.GAME_HEIGHT - 20, 20 + cornerSize, GameEngine.GAME_HEIGHT - 20);
            graphics.DrawLine(borderPen, 20, GameEngine.GAME_HEIGHT - 20, 20, GameEngine.GAME_HEIGHT - 20 - cornerSize);
            
            graphics.DrawLine(borderPen, GameEngine.GAME_WIDTH - 20, GameEngine.GAME_HEIGHT - 20, GameEngine.GAME_WIDTH - 20 - cornerSize, GameEngine.GAME_HEIGHT - 20);
            graphics.DrawLine(borderPen, GameEngine.GAME_WIDTH - 20, GameEngine.GAME_HEIGHT - 20, GameEngine.GAME_WIDTH - 20, GameEngine.GAME_HEIGHT - 20 - cornerSize);
            
            borderPen.Dispose();
        }
        
        private void DrawSelectionIndicator(Graphics graphics, float x, float y, float width)
        {
            // 绘制选中项的装饰性指示器
            Pen indicatorPen = new Pen(Color.FromArgb(200, 255, 69, 0), 2);
            graphics.DrawRectangle(indicatorPen, x, y - 5, width, 35);
            
            // 绘制箭头
            Point[] leftArrow = {
                new Point((int)x - 10, (int)y + 12),
                new Point((int)x - 5, (int)y + 7),
                new Point((int)x - 5, (int)y + 17)
            };
            Point[] rightArrow = {
                new Point((int)(x + width + 10), (int)y + 12),
                new Point((int)(x + width + 5), (int)y + 7),
                new Point((int)(x + width + 5), (int)y + 17)
            };
            
            graphics.FillPolygon(new SolidBrush(Color.FromArgb(200, 255, 69, 0)), leftArrow);
            graphics.FillPolygon(new SolidBrush(Color.FromArgb(200, 255, 69, 0)), rightArrow);
            
            indicatorPen.Dispose();
        }
        
        public void OnKeyDown(KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Up:
                    selectedOption = (selectedOption - 1 + menuOptions.Length) % menuOptions.Length;
                    break;
                case Keys.Down:
                    selectedOption = (selectedOption + 1) % menuOptions.Length;
                    break;
                case Keys.Enter:
                    HandleMenuSelection();
                    break;
                case Keys.Escape:
                    Application.Exit();
                    break;
            }
        }
        
        public void OnKeyUp(KeyEventArgs e)
        {
            // 主菜单不需要处理按键释放
        }
        
        private void HandleMenuSelection()
        {
            switch (selectedOption)
            {
                case 0: // 开始游戏
                    GameManager.Instance.ChangeState(GameState.GamePlay);
                    break;
                case 1: // 设置
                    GameManager.Instance.ChangeState(GameState.Settings);
                    break;
                case 2: // 排行榜
                    GameManager.Instance.ChangeState(GameState.Leaderboard);
                    break;
                case 3: // 退出游戏
                    Application.Exit();
                    break;
            }
        }
        
        public void Dispose()
        {
            titleFont?.Dispose();
            menuFont?.Dispose();
            selectedFont?.Dispose();
            titleBrush?.Dispose();
            menuBrush?.Dispose();
            selectedBrush?.Dispose();
            backgroundBrush?.Dispose();
        }
    }
} 